package com.sdry.service.impl.cb;

import com.sdry.mapper.cb.AmoebaMapper;
import com.sdry.model.cb.*;
import com.sdry.model.llm.LlmPrepaidExpenses;
import com.sdry.service.cb.AmoebaService;
import com.sdry.service.cb.HydroPowerService;
import com.sdry.utils.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Service
public class AmoebaServiceImpl implements AmoebaService {

    @Autowired
    private HydroPowerService hydroPowerService;

    @Autowired
    private AmoebaMapper amoebaMapper;

    @Override
    public List<AmoebaEntity> listPartA(Long stockId, String date) {
        List<AmoebaEntity> list = new ArrayList<>();

        List<HydroPower> hydroPowers = hydroPowerService.getList(stockId,date);
        for (HydroPower hp : hydroPowers) {
            AmoebaEntity ae = new AmoebaEntity();
            ae.setLevel1("收入（未税）（A）");
            ae.setLevel2("外部收入");
            ae.setDetail("房租水电");
            ae.setRemark("不含税价");
            ae.setPrice(hp.getPrice());
            ae.setLevel3(hp.getSupplier());
            //房租
            String multiply = BigDecimalUtil.multiply(hp.getWarehousequantity().toString(), hp.getPrice().toString());
            //水电费用
            String multiply1 = BigDecimalUtil.multiply(multiply, "0.16");
            ae.setMonery(new BigDecimal(BigDecimalUtil.add(multiply,multiply1)));
            list.add(ae);
        }

        date = date.replaceAll("[[\\s-:punct:]]","");

        AmoebaEntity amoebaEntity = amoebaMapper.selectAmoebaList(date,stockId);
        if (amoebaEntity == null){
            amoebaEntity = new AmoebaEntity();
        }
        amoebaEntity.setLevel2("内部部收入");
        amoebaEntity.setLevel1("收入（未税）（A）");
        amoebaEntity.setRemark("不含税价");
        list.add(amoebaEntity);
        return list;
    }

    @Override
    public BigDecimal gongZi(String date, Long stockId) {
        return amoebaMapper.gongZi(date,stockId);
    }


    @Override
    public BigDecimal fuWuFei(String date, Long stockId) {
        return amoebaMapper.fuWuFei(stockId,date);
    }

    @Override
    public BigDecimal sheBaoFei(String date, Long stockId) {
        return amoebaMapper.sheBaoFei(stockId,date);
    }


    @Override
    public List<ShareEntity> getShareList(String date, Long warehouseId) {
        return amoebaMapper.getShareList(date,warehouseId);
    }

    @Override
    public Integer deleteShare(String[] idArr) {
        Integer offectRow = 0;
        for (int i = 0; i < idArr.length; i++) {
            offectRow = amoebaMapper.setDelete(Long.parseLong(idArr[i]));
        }
        return offectRow;
    }

    @Override
    public List<LlmPrepaidExpenses> getAmortizationList(Long warehouseId, String dateOfOccurrence) {
        return amoebaMapper.getAmortizationList(warehouseId,dateOfOccurrence);
    }

    @Override
    public Integer amoebaSave(Long id, String parameter1, String parameter2, BigDecimal parameter3, BigDecimal parameter4, BigDecimal parameter5, String type) {
        //先查询有没有
        Long aid = amoebaMapper.selectAmoebaById(id);
        //没有的话，创建一个
        if (aid == null || aid == 0) {
            amoebaMapper.amoebaInsert(id,type);
        }
        //更新
        return amoebaMapper.amoebaUpdate(id,parameter1,parameter2,parameter3,parameter4,parameter5,type);
    }

    @Override
    public Integer amoebaUpdateByFZ(Long id, BigDecimal parameter5, BigDecimal parameter6, BigDecimal parameter7, BigDecimal parameter8, String type) {
        //先查询有没有
        Long aid = amoebaMapper.selectAmoebaById(id);
        //没有的话，创建一个
        if (aid == null || aid == 0) {
            amoebaMapper.amoebaInsert(id,type);
        }
        return amoebaMapper.amoebaUpdateByFZ(id,parameter5,parameter6,parameter7,parameter8,type);
    }

    @Override
    public CostUnit getList(Long stockId, String date) throws ParseException {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        CostUnit costUnit = new CostUnit();


        /////////////////////////自定义手动输入的数据///////////////////////////////////////////////////////////////

        AmoebaEntity amoebaEntity = amoebaMapper.selectAmoebaList(date.replaceAll("[[\\s-:punct:]]",""), stockId);

        if (amoebaEntity != null) {

            //房租
            costUnit.setParameter5(amoebaEntity.getParameter5());
            //物业
            costUnit.setParameter6(amoebaEntity.getParameter6());
            //水电
            costUnit.setParameter7(amoebaEntity.getParameter7());
            //公共分摊（b2）
            costUnit.setParameter8(amoebaEntity.getParameter8());
        }

        /////////////////A 上面的外部//////////////////////////////////////////////////////////////////////
        List<AmoebaEntity> amoebaEntities = this.listPartA(stockId, date);
        BigDecimal t1 = new BigDecimal("0");
        for (AmoebaEntity ae : amoebaEntities) {
            if (ae.getMonery() == null){
                ae.setMonery(new BigDecimal("0"));
            }
            BigDecimal t2 = ae.getMonery();
            String t3 = BigDecimalUtil.add(t1.toString(), t2.toString());
            t1 = new BigDecimal(t3);
        }
        costUnit.setPartAPrice(t1);
        //////////////////////////////////////////////////////////////////////////////////////////////////
        //货架
        costUnit.setHuoJia(amoebaMapper.huoJia(stockId,date));
        //叉车折旧
        BigDecimal chaCheZheJiu = amoebaMapper.chaCheZheJiu(stockId,date);
        costUnit.setChaCheZheJiu(chaCheZheJiu);
        //叉车租赁
        BigDecimal cheCheZuLin = amoebaMapper.cheCheZuLin(stockId,date);
        costUnit.setChaChezuLin(cheCheZuLin);
        //////////////////////////////////////////////////////////////////////////////////////////////////
        //工资
        date = date +"-01";
        costUnit.setGongZi(gongZi(date,stockId));
        //社保
        costUnit.setSheBaoFei(sheBaoFei(date,stockId));
        //服务费
        costUnit.setFuWuFei(fuWuFei(date,stockId));
        //分公司经理
        costUnit.setFenGongSiJingLi(new BigDecimal(0));
        //////////////////////////////////////////////////////////////////////////////////////////////////

        //查询月过桥费总和
        costUnit.setGuoQiaoFei(amoebaMapper.guoQiaoFei(date,stockId));
        //-查询月油费总和
        costUnit.setYouFei(amoebaMapper.youFei(date,stockId));
        //查询月维修费总和
        costUnit.setWeiXiuFei(amoebaMapper.weiXiuFei(date,stockId));
        //查询月外雇车总和
        costUnit.setWaiGuChe(amoebaMapper.waiGuChe(date,stockId));
        ///////////////////////////////////////////////////////////////////////////////////////////////////
         //待摊
        BigDecimal tanFei = amoebaMapper.daiTanFei(date, stockId)==null?new BigDecimal(0):amoebaMapper.daiTanFei(date, stockId);
        //分公司明细
        BigDecimal fenGongSiMingXi = amoebaMapper.fenGongSiMingXi(date, stockId)==null?new BigDecimal(0):amoebaMapper.fenGongSiMingXi(date, stockId);
        //折旧费
        List<DepreciationEntity> zheJiuFie = amoebaMapper.zheJiuFie(date, stockId);
        BigDecimal zheJiuFies = DepreciationServiceImpl.getBigDeMonery(zheJiuFie,format1.parse(date))==null?new BigDecimal(0):DepreciationServiceImpl.getBigDeMonery(zheJiuFie,format1.parse(date));
        //其他
        BigDecimal qiTa = amoebaMapper.qiTa(date, stockId)==null?new BigDecimal(0):amoebaMapper.qiTa(date, stockId);

        String add = BigDecimalUtil.add(BigDecimalUtil.add(tanFei.toString(), fenGongSiMingXi.toString()), BigDecimalUtil.add(zheJiuFies.toString(), qiTa.toString()));
        costUnit.setQiTa(new BigDecimal(add));



        return costUnit;
    }




}
